#include "algorithmeditdialog.h"
#include "ui_algorithmeditdialog.h"

AlgorithmEditDialog::AlgorithmEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AlgorithmEditDialog)
{
    ui->setupUi(this);

    ui->algoritmTableWidget->setColumnCount(4);
    ui->algoritmTableWidget->setHorizontalHeaderLabels(
            QStringList() << tr("Name") << tr("Optimization") << tr("DLL file") << tr("Help file") );

    algorithmInputDialog = 0;

    connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addAlgorithm()));
    connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteAlgorithm()));
    connect(ui->algoritmTableWidget, SIGNAL(cellClicked(int,int)),
            ui->algoritmTableWidget, SLOT(selectRow(int)));
    connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(updateAndClose()));
}

AlgorithmEditDialog::~AlgorithmEditDialog()
{
    delete ui;
}

void AlgorithmEditDialog::prepare()
{
    QTableWidget *tw = ui->algoritmTableWidget;
    tw->setRowCount(algorithms.count());
    for (int i=0; i<algorithms.count(); i++){
        tw->setItem(i, 0, new QTableWidgetItem(algorithms[i].name));
        tw->setItem(i, 1, new QTableWidgetItem(algorithms[i].optimization));
        tw->setItem(i, 2, new QTableWidgetItem(algorithms[i].dllPath));
        tw->setItem(i, 3, new QTableWidgetItem(algorithms[i].helpPath));
    }
}

void AlgorithmEditDialog::addAlgorithm()
{
    if (algorithmInputDialog == 0){
        algorithmInputDialog = new AlgorithmInputDialog(this);
        connect(algorithmInputDialog, SIGNAL(itemMade(Algorithm)),this, SLOT(itemMade(Algorithm)));
    }
    algorithmInputDialog->prepare();
    algorithmInputDialog->exec();
}

void AlgorithmEditDialog::deleteAlgorithm()
{
    int row = ui->algoritmTableWidget->currentRow();
    if (row < 0) {
        QMessageBox::warning(this, tr("Warning"), tr("Please select the row to delete"));
        return;
    }

    ui->algoritmTableWidget->removeRow(row);

    algorithms.removeAt(row);
}

void AlgorithmEditDialog::updateAndClose()
{
    QFile *file = new QFile(QDir::currentPath() + "/algorithms.list");
    file->open(QIODevice::WriteOnly);

    QDataStream out(file);
    out << algorithms;
    file->close();

    AlgorithmStrings.clear();
    OptimizationStrings.clear();
    for (int i=0; i<algorithms.count(); i++){
        if (!AlgorithmStrings.contains(algorithms[i].name))
            AlgorithmStrings << algorithms[i].name;
        if (!OptimizationStrings.contains(algorithms[i].optimization))
            OptimizationStrings << algorithms[i].optimization;
    }

    this->close();
}

void AlgorithmEditDialog::itemMade(Algorithm algorithm)
{
    QTableWidget *tw = ui->algoritmTableWidget;
    tw->insertRow(0);
    tw->setItem(0, 0, new QTableWidgetItem(algorithm.name));
    tw->setItem(0, 1, new QTableWidgetItem(algorithm.optimization));
    tw->setItem(0, 2, new QTableWidgetItem(algorithm.dllPath));
    tw->setItem(0, 3, new QTableWidgetItem(algorithm.helpPath));

    tw->resizeColumnsToContents();
    tw->resizeRowsToContents();

    algorithms << algorithm;

    if (!AlgorithmStrings.contains(algorithm.name)) AlgorithmStrings << algorithm.name;
    if (!OptimizationStrings.contains(algorithm.optimization)) OptimizationStrings << algorithm.optimization;
}
